import { defineStore } from 'pinia'
//引入类型约束文件
import type {categoryStateModelList} from '@/api/product/model/categoryModel'
//引入接口函数
import {
    reqCategorySelector1List
    , reqCategorySelector2List,
    reqCategorySelector3List
}
    from '@/api/product/categorySelector'
//创建模块对象
export const useCategoryListStore = defineStore('categoryList', {
    state: ():categoryStateModelList => {
        return {
            category1Id: undefined,
            category2Id: undefined,
            category3Id: undefined,

            //用来接收服务器响应回来的数据
            category1List: [],
            category2List: [],
            category3List: [],
        }
    },
    actions: {
        //获取一级分类数据
        async getCategory1ListData() {

            const result = await reqCategorySelector1List()
            // console.log('aa',result)
            this.category1List = result
        },
        //获取二级分类
        async getCategory2ListData(category1Id: number) {
            this.category1Id = category1Id
            //数据联动
            this.category2Id = undefined
            this.category3Id = undefined
            this.category3List = []
            const result = await reqCategorySelector2List(category1Id)

            this.category2List = result

        },
        //获取三级分类
        async getCategory3ListData(category2Id: number) {

            //数据联动
            this.category3Id = undefined
            this.category2Id = category2Id

            const result = await reqCategorySelector3List(category2Id)

            this.category3List = result

        },
        //设置三级分类的id 
        setCategory3Id(category3Id:number) {
            this.category3Id = category3Id
        }
    }

})
